home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / DirectPlay / Tutorials / Tut01_EnumSP / EnumSP.cpp next >
C/C++ Source or Header  |  2001-10-08  |  5KB  |  161 lines

  1. //----------------------------------------------------------------------------
  2. // File: EnumSP.cpp
  3. //
  4. // Desc: This simple program inits DirectPlay and enumerates the available
  5. //       DirectPlay Service Providers.
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define INITGUID
  10. #define _WIN32_DCOM
  11. #include <stdio.h>
  12. #include <dplay8.h>
  13.  
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Global variables
  17. //-----------------------------------------------------------------------------
  18. IDirectPlay8Peer* g_pDP = NULL;    // DirectPlay peer object
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Function-prototypes
  23. //-----------------------------------------------------------------------------
  24. HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);
  25. HRESULT InitDirectPlay();
  26. void    CleanupDirectPlay();
  27.  
  28.  
  29. //-----------------------------------------------------------------------------
  30. // Miscellaneous helper functions
  31. //-----------------------------------------------------------------------------
  32. #define SAFE_DELETE(p)       { if(p) { delete (p);     (p)=NULL; } }
  33. #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p);   (p)=NULL; } }
  34. #define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }
  35.  
  36.  
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Name: main()
  41. // Desc: Entry point for the application.  
  42. //-----------------------------------------------------------------------------
  43. int main(int argc, char* argv[], char* envp[])
  44. {
  45.     HRESULT                     hr;
  46.     DPN_SERVICE_PROVIDER_INFO*  pdnSPInfo       = NULL;
  47.     DPN_SERVICE_PROVIDER_INFO*  pdnSPInfoEnum   = NULL;
  48.     DWORD                       dwItems         = 0;
  49.     DWORD                       dwSize          = 0;
  50.     DWORD                       i;
  51.  
  52.     // Init COM so we can use CoCreateInstance
  53.     CoInitializeEx(NULL, COINIT_MULTITHREADED);
  54.  
  55.     // Init the DirectPlay system
  56.     if( FAILED( hr = InitDirectPlay() ) )
  57.     {
  58.         printf("Failed Initializing DirectPlay:  0x%X\n", hr);
  59.         goto LCleanup;
  60.     }
  61.  
  62.     // Enumerate all the Service Providers available
  63.     hr = g_pDP->EnumServiceProviders(NULL, NULL, NULL, &dwSize, &dwItems, 0);
  64.  
  65.     if( hr != DPNERR_BUFFERTOOSMALL)
  66.     {
  67.         printf("Failed Enumerating Service Providers:  0x%X\n", hr);
  68.         goto LCleanup;
  69.     }
  70.  
  71.     pdnSPInfo = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[dwSize];
  72.     
  73.     if( FAILED( hr = g_pDP->EnumServiceProviders(NULL, NULL, pdnSPInfo, &dwSize, &dwItems, 0 ) ) )
  74.     {
  75.         printf("Failed Enumerating Service Providers:  0x%X\n", hr);
  76.         goto LCleanup;
  77.     }
  78.  
  79.     // Run through each provider desc printing them all out
  80.     pdnSPInfoEnum = pdnSPInfo;
  81.     for (i = 0; i < dwItems; i++)
  82.     {
  83.         printf("Found Service Provider:  %S\n", pdnSPInfoEnum->pwszName);
  84.  
  85.         pdnSPInfoEnum++;
  86.     }
  87.  
  88. LCleanup:
  89.  
  90.     SAFE_DELETE_ARRAY(pdnSPInfo);
  91.  
  92.     CleanupDirectPlay();
  93.     
  94.     // Cleanup COM
  95.     CoUninitialize();
  96.  
  97.     return 0;
  98. }
  99.  
  100.  
  101.  
  102.  
  103. //-----------------------------------------------------------------------------
  104. // Name: DirectPlayMessageHandler
  105. // Desc: Handler for DirectPlay messages.  This tutorial doesn't repond to any
  106. //       DirectPlay messages
  107. //-----------------------------------------------------------------------------
  108. HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, 
  109.                                          PVOID pMsgBuffer)
  110. {
  111.     return S_OK;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. //-----------------------------------------------------------------------------
  118. // Name: InitDirectPlay()
  119. // Desc: Initialize DirectPlay
  120. //-----------------------------------------------------------------------------
  121. HRESULT InitDirectPlay()
  122. {
  123.     HRESULT hr = S_OK;
  124.  
  125.     // Create the IDirectPlay8Peer Object
  126.     if( FAILED( hr = CoCreateInstance( CLSID_DirectPlay8Peer, NULL, 
  127.                                        CLSCTX_INPROC_SERVER,
  128.                                        IID_IDirectPlay8Peer, 
  129.                                        (LPVOID*) &g_pDP ) ) )
  130.     {
  131.         printf("Failed Creating the IDirectPlay8Peer Object:  0x%X\n", hr);
  132.         goto LCleanup;
  133.     }
  134.  
  135.     // Init DirectPlay
  136.     if( FAILED( hr = g_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) ) )
  137.     {
  138.         printf("Failed Initializing DirectPlay:  0x%X\n", hr);
  139.         goto LCleanup;
  140.     }
  141.     
  142. LCleanup:
  143.     return hr;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. //-----------------------------------------------------------------------------
  150. // Name: CleanupDirectPlay()
  151. // Desc: Cleanup DirectPlay
  152. //-----------------------------------------------------------------------------
  153. void CleanupDirectPlay()
  154. {
  155.     // Cleanup DirectPlay
  156.     if( g_pDP)
  157.         g_pDP->Close(0);
  158.  
  159.     SAFE_RELEASE(g_pDP);
  160. }
  161.